package com.newupbank.schedule.aliyun.task;

import cn.hutool.core.collection.CollectionUtil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordRequest;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.newupbank.schedule.aliyun.vo.IpInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @author: liminghui
 * @date: 2020/12/18 23:18
 * @version: 1.0
 * @description: 获取域名/更新域名
 */
@Component
public class OperateDomain {
    private static final Logger logger = LoggerFactory.getLogger(OperateDomain.class);
    @Value("${accessKeyId}")
    private String accessKeyId;
    @Value("${accessSecret}")
    private String accessSecret;
    @Value("${domainName}")
    private String domainName;

    // 获取本机对应的远端域名信息
    public DescribeDomainRecordsResponse.Record loadDomain(IpInfoVO ipInfoVO) {
        // aliyun 认证
        // 需要将下载的 accessKeyId、accessSecret 替换下面的 <accessKeyId>、<accessSecret>
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        // 获取解析记录列表
        DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest();
        // 这里填各位自己的域名，填错域名将不能正确获取信息
        request.setDomainName(domainName);

        try {
            // 提交需要取信息的域名
            // 并得到返回结果
            DescribeDomainRecordsResponse response = client.getAcsResponse(request);
            List<DescribeDomainRecordsResponse.Record> domainRecords = response.getDomainRecords();
            if (CollectionUtil.isNotEmpty(domainRecords)) {
                for (DescribeDomainRecordsResponse.Record domainRecord : domainRecords) {
                    if (domainRecord.getRR().equals(ipInfoVO.getRR()))
                        return domainRecord;
                }

            }
        } catch (ClientException e) {
            logger.error("LoadDomain===>loadDomain is error!", e);
        }
        return null;
    }


    // 更新域名ip
    public UpdateDomainRecordResponse updateDomain(DescribeDomainRecordsResponse.Record record) {
        UpdateDomainRecordResponse response = null;
        // aliyun 认证
        // 需要替换 <accessKeyId> <accessSecret>
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        // 更新域名解析
        UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();

        // 某个解析的唯一编号，在上一个测试中获取
        request.setRecordId(record.getRecordId());
        // 解析的类型
        request.setType(record.getType());
        // // 二级域名
        request.setRR(record.getRR());
        // // 新的 IP 地址 (这里我随便写了个地址)
        request.setValue(record.getValue());
        // // DNS更新时间 (单位秒，最少10分钟)
        // request.setTTL(record.getTTL());
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            logger.error("UpdateDomain===>updateDomain is error", e);
        }
        return response;
    }

}
